{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "raw_mimetype": "text/restructuredtext"
   },
   "source": [
    ".. _nb_initialization:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Biased Initialization\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "One way of customizing an algorithm is a biased initial population. This can be very helpful if expert knowledge already exists, and known solutions should be improved. In the following, two different ways of initialization are provided: **a)** just providing the design space of the variables and **b)** a `Population` object where the objectives and constraints are provided and are not needed to be calculated again.\n",
    "\n",
    "**NOTE:** This works with all **population-based** algorithms in *pymoo*. Technically speaking, all algorithms which inherit from `GeneticAlgorithm`. For **local-search** based algorithm, the initial solution can be provided by setting `x0` instead of `sampling`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## By Array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "code": "algorithms/usage_initial_array.py"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "============================================================\n",
      "n_gen |  n_eval |     igd      |      gd      |      hv     \n",
      "============================================================\n",
      "    1 |     300 |  3.557557465 |  3.560881169 |  0.00000E+00\n",
      "    2 |     400 |  3.557557465 |  3.551066633 |  0.00000E+00\n",
      "    3 |     500 |  3.275216088 |  3.448549183 |  0.00000E+00\n",
      "    4 |     600 |  3.198628664 |  3.176812598 |  0.00000E+00\n",
      "    5 |     700 |  2.841616722 |  3.303868158 |  0.00000E+00\n",
      "    6 |     800 |  2.814190050 |  3.055198410 |  0.00000E+00\n",
      "    7 |     900 |  2.534272391 |  2.999948132 |  0.00000E+00\n",
      "    8 |    1000 |  2.390725227 |  2.395462074 |  0.00000E+00\n",
      "    9 |    1100 |  2.125710695 |  2.377701247 |  0.00000E+00\n",
      "   10 |    1200 |  2.123601951 |  2.097040176 |  0.00000E+00\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<pymoo.model.result.Result at 0x7f9fd252e910>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "from pymoo.algorithms.nsga2 import NSGA2\n",
    "from pymoo.factory import get_problem\n",
    "from pymoo.optimize import minimize\n",
    "\n",
    "problem = get_problem(\"zdt2\")\n",
    "\n",
    "X = np.random.random((300, problem.n_var))\n",
    "\n",
    "algorithm = NSGA2(pop_size=100, sampling=X)\n",
    "\n",
    "minimize(problem,\n",
    "         algorithm,\n",
    "         ('n_gen', 10),\n",
    "         seed=1,\n",
    "         verbose=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## By Population (pre-evaluated)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "code": "algorithms/usage_initial_pop.py"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "============================================================\n",
      "n_gen |  n_eval |     igd      |      gd      |      hv     \n",
      "============================================================\n",
      "    1 |       0 |  3.322862696 |  3.606142195 |  0.00000E+00\n",
      "    2 |     100 |  3.322862696 |  3.585870314 |  0.00000E+00\n",
      "    3 |     200 |  3.225049918 |  3.346862599 |  0.00000E+00\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    4 |     300 |  2.664109526 |  3.342549818 |  0.00000E+00\n",
      "    5 |     400 |  2.573127159 |  3.171875277 |  0.00000E+00\n",
      "    6 |     500 |  2.573127159 |  3.077690887 |  0.00000E+00\n",
      "    7 |     600 |  2.382630541 |  2.827424111 |  0.00000E+00\n",
      "    8 |     700 |  2.229156182 |  2.743251373 |  0.00000E+00\n",
      "    9 |     800 |  2.142758472 |  2.567058186 |  0.00000E+00\n",
      "   10 |     900 |  2.103682698 |  2.475123347 |  0.00000E+00\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<pymoo.model.result.Result at 0x7f9fd32b2910>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "from pymoo.algorithms.nsga2 import NSGA2\n",
    "from pymoo.factory import get_problem\n",
    "from pymoo.model.evaluator import Evaluator\n",
    "from pymoo.model.population import Population\n",
    "from pymoo.optimize import minimize\n",
    "\n",
    "problem = get_problem(\"zdt2\")\n",
    "\n",
    "# create initial data and set to the population object\n",
    "X = np.random.random((300, problem.n_var))\n",
    "pop = Population.new(\"X\", X)\n",
    "Evaluator().eval(problem, pop)\n",
    "\n",
    "algorithm = NSGA2(pop_size=100, sampling=pop)\n",
    "\n",
    "minimize(problem,\n",
    "         algorithm,\n",
    "         ('n_gen', 10),\n",
    "         seed=1,\n",
    "         verbose=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
